
import org.deltaset.support.gen.GenFactory;

class CommandProcessor{ 

  
    
    static void process(project){
        def cmdString = project.properties['cmd'];
        def commandNameAndContext = cmdString.split("=");       
        def commandName = commandNameAndContext[0];
        println(commandNameAndContext.size())
        def testDsonString="";//"Book:[@extends:BaseEntity,age:[@type:long,@NotNull:true,@min:3,@max:5],chapters:[@type:List<Chapter:[@extends:BaseChild, title,content:[@NotNull:true]]>],name,notes]";
        def commandContext = (commandNameAndContext.size()<2)?testDsonString:commandNameAndContext[1];
        
        
        println("------------CommandProcessor--------------");
        println("       commandName ="+commandName);
        println("    commandContext ="+commandContext);
        println("------------------------------------------");   
        
        if("file".equals(commandName)){
            FileCommand.execute(project,commandContext)
        }else{
            executeCommand(project, commandName, commandContext);
        }

    }
    
    static def executeCommand(project, commandName, commandContext){
        
        def packageName = project.properties['packageName']//project.groupId
        def baseDir = project.basedir
        def templateDir = project.properties['templateDir']
        boolean overwriteFlag = ("true"==project.properties['ow']);
    
        def factory = GenFactory.config()
            .packageName(packageName)
            .baseDir(baseDir)
            .templateDir(templateDir)
            .overwrite(overwriteFlag)
            .add( "enum", TemplateMaps.enumMap() )
            .add( "domain", TemplateMaps.domain() )
            .add( "repository", TemplateMaps.repository() )
            .add( "simple-bean", TemplateMaps.simpleJsfBean() )
            .add( "converter", TemplateMaps.converter() )
            .add( "resource", TemplateMaps.resource() )
                    
            .add( "form", TemplateMaps.form() )
            .add( "detail", TemplateMaps.detail() )
            .add( "list", TemplateMaps.list() )
            .add( "many-to-one", TemplateMaps.manyToOne() )

            .add( "test", TemplateMaps.test() )
                                        
            .add("labels", new LabelWriter() )
            .add("menu", new MenuWriter() )
            .add("persistence" , new PersistenceWriter() )
            .add("crud",["domain","repository","simple-bean","form","detail","list","many-to-one","converter","resource","labels","menu","persistence"])
        .finish();
        
        factory.execute(commandName,commandContext);
    }
}